package ee.ut.aa.neuraltic.neural;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

public abstract class Layer {

	private static Logger log = Logger.getLogger( Layer.class );

	private Layer nextLayer;
	private List<Neuron> neurons;

	public Layer( Layer nextLayer ) {

		log.debug( "Initializing layer." );

		this.nextLayer = nextLayer;
	}

	public void initNeurons( int size ) {

		log.debug( "Initializing layer neurons." );

		neurons = new ArrayList<Neuron>();

		Neuron neuron;

		for( int i = 0; i < size; i++ ) {

			neuron = new Neuron();
			neurons.add( neuron );
		}
	}

	public void initFeedForward() {

		log.debug( "Initializing layer feed-forward." );

		for( Neuron neuron : neurons ) {
			
			if( neuron.getValue() < 0 )
				continue;
			
			for( Synaps synaps : neuron.getSynapses() ) {

				Neuron dest = synaps.getNeuron();
				double oldValue = dest.getValue();

				// TODO: It's that simple???
				dest.setValue( oldValue + neuron.getValue() * synaps.getWeight() );
			}
		}

		if( nextLayer != null )
			nextLayer.initFeedForward();
	}

	public Layer getNextLayer() {
		return nextLayer;
	}

	public void setNextLayer( Layer nextLayer ) {
		this.nextLayer = nextLayer;
	}

	public List<Neuron> getNeurons() {
		return neurons;
	}

	public void setNeurons( List<Neuron> neurons ) {
		this.neurons = neurons;
	}

	@Override
	public String toString() {

		String result = "\n";

		for( Neuron neuron : neurons ) {
			result += "  [" + neuron + "];\n";
		}
		return result;
	}
}
